﻿namespace _135分发糖果
{
    internal class Program
    {
        static void Main(string[] args)
        {
            int[] a = [1, 0, 2];
            int max = Solution.Candy(a);
            Console.WriteLine(max);
        }
    }
    public class Solution
    {
        public static int Candy(int[] ratings)
        {
            int n = ratings.Length;
            //默认所有糖果都是1
            int[] candy = new int[ratings.Length];
            for (int i = 0; i < ratings.Length; i++)
            {
                candy[i] = 1;
            }
            for (int i = 1; i < ratings.Length; i++)
            {
                if (ratings[i] > ratings[i-1])
                {
                    candy[i]= candy[i-1] + 1;
                }
            }
            for (int i = n - 2; i >= 0; i--)
            {
                if (ratings[i] > ratings[i + 1] && candy[i] <= candy[i + 1])
                {
                    candy[i] = candy[i + 1] + 1;
                }
            }         
            return candy.Sum();
        }
    }
}
